package com.liddhome.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.liddhome.entity.Essay;
import com.liddhome.entity.PageBean;
import com.liddhome.service.EssayService;
import com.liddhome.util.PageUtil;
import com.liddhome.util.StringUtil;

/**
 * 心情随笔Controller层
 * @author lidd
 *
 */
@Controller
@RequestMapping("/")
public class EssayController {

	@Resource
	private EssayService essayService;
	
	/**
	 * 请求主页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/essay")
	public ModelAndView index(@RequestParam(value="page",required=false)String page,HttpServletRequest request)throws Exception{
		ModelAndView mav=new ModelAndView();
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),10);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Essay> essayList=essayService.list(map);
		mav.addObject("essayList", essayList);
		StringBuffer param=new StringBuffer(); // 查询参数
		mav.addObject("pageCode",PageUtil.genPagination(request.getContextPath()+"/essay.html", essayService.getTotal(map), Integer.parseInt(page), 10, param.toString()));
		mav.addObject("mainPage", "foreground/blog/essay.jsp");
		mav.addObject("pageTitle","LiddBlog");
		mav.setViewName("mainTemp");
		return mav;
	}
}
